package com.struts2.service;

import java.util.List;




import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.struts2.model.Category;
import com.struts2.model.Color;
import com.struts2.model.Customer;
import com.struts2.model.Designer;
import com.struts2.model.Gender;
import com.struts2.model.Product_item;
import com.struts2.model.Product_line;
import com.struts2.model.Season;
import com.struts2.model.Size;
import com.struts2.model.Type;

public class ProductViewService {

	public List<Product_line> GetProductList(List<Product_line> product_line,
			int productPerPage, int pageNum, String gender, String type,
			String category, String username) {

		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Product_item.class,
				"product_item");
		criteria.createAlias("product_item.product_line", "product_line");

		if (!gender.equals("")) {
			criteria.createAlias("product_line.gender", "gender");
			criteria.add(Restrictions.eq("gender.name", gender));
		}
		if (!category.equals("")) {
			criteria.createAlias("product_line.category", "category");
			criteria.add(Restrictions.eq("category.name", category));
		}
		if (!type.equals("")) {
			criteria.createAlias("product_line.type", "type");
			criteria.add(Restrictions.eq("type.name", type));
		}
		if (!username.equals("")) {
			criteria.createAlias("product_line.designer", "designer");
			criteria.add(Restrictions.eq("designer.username", username));
		}
		criteria.addOrder(Order.asc("approval"));
		criteria.addOrder(Order.asc("id"));
		
		//criteria.add(Restrictions.eq("approval", 1));

		criteria.setProjection(Projections.distinct(Projections
				.projectionList()

				.add(Projections.property("product_line"))));

		criteria.setFirstResult(productPerPage * (pageNum - 1));
		criteria.setMaxResults(productPerPage);
		product_line = (List<Product_line>) criteria.list();

		session.getTransaction().commit();
		session.close();
		sF.close();
		return product_line;
	}

	public int GetProductCount(List<Product_line> product_items, String gender,
			String type, String category,String username) {

		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();

		// for delete , update, insert,||| not select
		// session.beginTransaction();

		Criteria criteria = session.createCriteria(Product_item.class,
				"product_item");
		criteria.createAlias("product_item.product_line", "product_line");

		if (!gender.equals("")) {
			// criteria.setFetchMode("gender", FetchMode.JOIN);
			criteria.createAlias("product_line.gender", "gender");
			criteria.add(Restrictions.eq("gender.name", gender));
		}
		if (!category.equals("")) {
			criteria.createAlias("product_line.category", "category");
			criteria.add(Restrictions.eq("category.name", category));
		}
		if (!type.equals("")) {
			criteria.createAlias("product_line.type", "type");
			criteria.add(Restrictions.eq("type.name", type));
		}
		if (!username.equals("")) {
			criteria.createAlias("product_line.designer", "designer");
			criteria.add(Restrictions.eq("designer.username", username));
		}

	//	criteria.add(Restrictions.eq("approval", 1));

		criteria.setProjection(Projections.distinct(Projections
				.property("product_line")));

		int productCount = criteria.list().size();

		session.getTransaction().commit();
		session.close();
		sF.close();

		return productCount;

	}

	public List<String> getLeftSideMenu(List<String> leftSideMenuList,
			String gender, String type, String category) {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Category.class, "category");
		if (category.equals("Accessories")) {
			// criteria.createAlias("category.type", "type");
			criteria.add(Restrictions.eq("name", category));
			criteria.setProjection(Projections.property("name"));
			leftSideMenuList = (List<String>) criteria.list();
		} else {
			criteria = session.createCriteria(Gender.class, "gender");
			criteria.createAlias("gender.category", "category");
			criteria.add(Restrictions.eq("gender.name", gender));
			criteria.setProjection(Projections.property("category.name"));
			leftSideMenuList = (List<String>) criteria.list();
		}

		// getLeftSideMenu(leftSideMenuList, gender, type, category);
		// criteria.setProjection(Projections.distinct(Projections
		// .property("product")));

		session.getTransaction().commit();
		session.close();
		sF.close();
		return leftSideMenuList;
	}

	public List<String> getLeftSubSideMenu(List<String> leftSideSubMenuList,
			String gender, String type, String category) {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();

		if (!gender.equals("")) {
			Criteria criteria = session.createCriteria(Gender.class, "gender");

			criteria.createAlias("gender.category", "category");

			criteria.createAlias("category.type", "type");
			criteria.add(Restrictions.eq("gender.name", gender));

			criteria.add(Restrictions.eq("category.name", category));
			criteria.setProjection(Projections.property("type.name"));
			leftSideSubMenuList = (List<String>) criteria.list();
			System.out.println("a" + gender + "n");
		} else {
			Criteria criteria = session.createCriteria(Category.class,
					"category");
			criteria.createAlias("category.type", "type");
			criteria.add(Restrictions.eq("category.name", category));
			criteria.setProjection(Projections.property("type.name"));
			leftSideSubMenuList = (List<String>) criteria.list();
			System.out.println("b");

		}

		// getLeftSideMenu(leftSubSideMenuList, gender, type, category);
		// criteria.setProjection(Projections.distinct(Projections
		// .property("product")));

		session.getTransaction().commit();
		session.close();
		sF.close();
		return leftSideSubMenuList;
	}

	public List<Designer> getDesigners(List<Designer> designer) {

		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Designer.class);
		criteria.addOrder(Order.asc("approval"));
		designer = (List<Designer>) criteria.list();

		session.getTransaction().commit();
		session.close();
		sF.close();
		return designer;
	}

	public Designer getDesigner(int id) {

		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Designer.class);
		criteria.add(Restrictions.eq("id", id));

		Designer designer = (Designer) criteria.list().get(0);

		session.getTransaction().commit();
		session.close();
		sF.close();
		return designer;
	}

	public void setDesigner(int id, String name, String background, String address, String gender,
			String email, int phone_number, String education,
			Long bank_account_number, String username, boolean approval) {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Designer.class);
		criteria.add(Restrictions.eq("id", id));

		Designer designer = (Designer) criteria.list().get(0);

		designer.setName(name);
		designer.setAddress(address);
		designer.setApproval(approval);
		designer.setBackground(background);
		designer.setBank_account_number(bank_account_number);
		designer.setEducation(education);
		designer.setEmail(email);
		designer.setGender(gender);
		designer.setPhone_number(phone_number);
		designer.setUsername(username);
		

		// product_item.setApproval(true);
		// session.save(season);

		session.save(designer);

		session.getTransaction().commit();
		session.close();
		sF.close();
	}

	public Product_line getProduct_line(int id) {

		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Product_line.class);
		criteria.add(Restrictions.eq("id", id));
	//	criteria.addOrder(Order.desc("approval"));
		Product_line product_line = (Product_line) criteria.list().get(0);

		session.getTransaction().commit();
		session.close();
		sF.close();
		return product_line;
	}

	public List<Product_item> getProduct_items(Product_line product_line) {

		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Product_item.class);
		criteria.add(Restrictions.eq("product_line", product_line));

		List<Product_item> product_items = (List<Product_item>) criteria.list();

		session.getTransaction().commit();
		session.close();
		sF.close();

		return product_items;

	}

	public List<Season> getSeasonList() {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Season.class);

		List<Season> seasonList = criteria.list();

		session.getTransaction().commit();
		session.close();
		sF.close();

		return seasonList;

	}

	public List<Gender> getGenderList() {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Gender.class);

		List<Gender> genderList = criteria.list();

		session.getTransaction().commit();
		session.close();
		sF.close();

		return genderList;
	}

	public List<Type> getTypeList() {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Type.class);

		List<Type> typeList = criteria.list();

		session.getTransaction().commit();
		session.close();
		sF.close();

		return typeList;
	}

	public List<Category> getCategoryList() {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Category.class);

		List<Category> categoryList = criteria.list();

		session.getTransaction().commit();
		session.close();
		sF.close();

		return categoryList;
	}

	public List<Designer> getDesignerList() {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Designer.class);

		List<Designer> designerList = criteria.list();

		session.getTransaction().commit();
		session.close();
		sF.close();

		return designerList;
	}

	public void setProduct(int id, String name, int price, int seasonId,
			int genderId, int typeId, int categoryId, int designerId) {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Product_line.class);
		criteria.add(Restrictions.eq("id", id));

		Product_line product_line = (Product_line) criteria.list().get(0);

		System.out.print(seasonId + "\n" + id);
		// /////////////////////////////
		Criteria criteriaSeason = session.createCriteria(Season.class);
		criteriaSeason.add(Restrictions.eq("id", seasonId));

		Season season = (Season) criteriaSeason.list().get(0);

		Criteria criteriaGender = session.createCriteria(Gender.class);
		criteriaGender.add(Restrictions.eq("id", genderId));

		Gender gender = (Gender) criteriaGender.list().get(0);

		Criteria criteriaType = session.createCriteria(Type.class);
		criteriaType.add(Restrictions.eq("id", typeId));

		Type type = (Type) criteriaType.list().get(0);

		Criteria criteriaCategory = session.createCriteria(Category.class);
		criteriaCategory.add(Restrictions.eq("id", categoryId));

		Category category = (Category) criteriaCategory.list().get(0);

		Criteria criteriaDesigner = session.createCriteria(Designer.class);
		criteriaDesigner.add(Restrictions.eq("id", designerId));

		Designer designer = (Designer) criteriaDesigner.list().get(0);

		product_line.setSeason(season);
		product_line.setGender(gender);
		product_line.setType(type);
		product_line.setCategory(category);
		product_line.setDesigner(designer);
		product_line.setName(name);
		product_line.setPrice(price);

		// product_item.setApproval(true);
		// session.save(season);

		session.save(product_line);

		session.getTransaction().commit();
		session.close();
		sF.close();
	}

	public Product_item getProduct_item(int id) {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Product_item.class);
		criteria.add(Restrictions.eq("id", id));

		Product_item product_item = (Product_item) criteria.list().get(0);
		session.getTransaction().commit();
		session.close();
		sF.close();
		return product_item;
	}

	public List<Product_line> getProduct_lineList() {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Product_line.class);

		List<Product_line> product_lineList = criteria.list();

		session.getTransaction().commit();
		session.close();
		sF.close();

		return product_lineList;

	}

	public List<Color> getColorList() {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Color.class);

		List<Color> colorList = criteria.list();

		session.getTransaction().commit();
		session.close();
		sF.close();

		return colorList;

	}

	public List<Size> getSizeList() {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Size.class);

		List<Size> sizeList = criteria.list();

		session.getTransaction().commit();
		session.close();
		sF.close();

		return sizeList;

	}

	public void setProduct_Item(int id, int product_lineId, int colorId,
			int sizeId, int quantity, int approval) {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Product_item.class);
		criteria.add(Restrictions.eq("id", id));

		Product_item product_item = (Product_item) criteria.list().get(0);

		Criteria criteriaProduct_line = session
				.createCriteria(Product_line.class);
		criteriaProduct_line.add(Restrictions.eq("id", product_lineId));
		Product_line product_line = (Product_line) criteriaProduct_line.list()
				.get(0);

		Criteria criteriaColor = session.createCriteria(Color.class);
		criteriaColor.add(Restrictions.eq("id", colorId));
		Color color = (Color) criteriaColor.list().get(0);

		Criteria criteriaSize = session.createCriteria(Size.class);
		criteriaSize.add(Restrictions.eq("id", sizeId));
		Size size = (Size) criteriaSize.list().get(0);

		product_item.setColor(color);
		product_item.setProduct_line(product_line);
		product_item.setSize(size);
		product_item.setApproval(approval);
		product_item.setQuantity(quantity);

		session.save(product_item);

		session.getTransaction().commit();
		session.close();
		sF.close();
	}

	
	
	
	public int getProductNotApproval()
	{
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Product_item.class);
		criteria.add(Restrictions.eq("approval", 0));
		
		int num = criteria.list().size();
		
		session.getTransaction().commit();
		session.close();
		sF.close();
		return num;
	}
	
	
	public int getDesignerNotApproval()
	{
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Designer.class);
		criteria.add(Restrictions.eq("approval", false));
		
		int num = criteria.list().size();
		
		session.getTransaction().commit();
		session.close();
		sF.close();
		return num;
	}

	
	public List<Customer> getCustomers()
	{
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Customer.class);
		
		List<Customer> customer =criteria.list();
		
		session.getTransaction().commit();
		session.close();
		sF.close();
		return customer;
	}
	

	public Customer getCustomer(int id) {

		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Customer.class);
		criteria.add(Restrictions.eq("id", id));

		Customer customer = (Customer) criteria.list().get(0);

		session.getTransaction().commit();
		session.close();
		sF.close();
		return customer;
	}
	
	
	public void setCustomer(int id, String name, String address, String gender,
			String email, int phone_number) {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Customer.class);
		criteria.add(Restrictions.eq("id", id));

		Customer customer = (Customer) criteria.list().get(0);

		customer.setName(name);
		customer.setAddress(address);
		customer.setEmail(email);
		customer.setGender(gender);
		customer.setPhone_number(phone_number);
		

		// product_item.setApproval(true);
		// session.save(season);

		session.save(customer);

		session.getTransaction().commit();
		session.close();
		sF.close();
	}
	
	
}
